$(function () {
    var goods = $('#goods');
    var shopLists = LsyStorage.getArr('shopList');
    var sortDatas=sortDatas?sortDatas:shopLists;
    var numMin= 0,numMax=0;
    /*登录验证*/
    loginTest();
    /*商品筛选*/
    linkAge();
    open();
    judgeSearch();
    goodsScreen();
    addCarEvent();
    footerSearch();
    inputNum('#text');

    /*三级联动*/
    function linkAge(){
        var prov = $('#prov');
        var city = $('#city');
        var county = $('#county');
        var provStr='<option value="0">--请选择省--</option>';
        for(var i=0;i<PRO.length;i++){
            provStr+='<option value="'+PRO[i].ProID+'">'+PRO[i].name+'</option>'
        }
        prov.html(provStr);
        prov.change(function(){
            var val=Number($(this).val());
            var citys=getCitysByProId(val);
            var cityStr='<option value="0">--请选择市--</option>';
            for(var i=0;i<citys.length;i++){
                cityStr+='<option value="'+citys[i].CityID+'">'+citys[i].name+'</option>'
            }
            city.html(cityStr);
        });
        city.change(function(){
            var val=Number($(this).val());
            var countys=getCountyByCityId(val);
            var countyStr='<option value="0">--请选择县--</option>';
            for(var i=0;i<countys.length;i++){
                countyStr+='<option>'+countys[i].DisName+'</option>'
            }
            county.html(countyStr);
        });
    }
    /*点击展开*/
    function open(){
        var launch=$('#open');
        var brand=$('#brand');
        var bool=false;
        brand.find('.show').nextAll().css("display","none");
        var lang=brand.find('ul').height()+15;
        brand.find('div').css("height",lang);
        launch.click(function(){
            if(!bool){
                brand.find('.show').nextAll().css("display","block");
                bool=true;
                launch.html('关闭<b>&lt</b>');
            }else {
                brand.find('.show').nextAll().css("display","none");
                bool=false;
                launch.html('展开<b>&gt</b>');
            }
            lang=brand.find('ul').height()+15;
            brand.find('div').css("height",lang);
        })
    }
    /*排序*/
    function goodsScreen() {
        var sort_i = $('#sort>div:first-child i');
        var price = $('#price');
        var defaultSort=$('#default');
        var sure=$('#sure');
        sort_i.click(function () {
            $(this).addClass('on').siblings().removeClass('on');
            if($(this).attr("order")=="true"){
                $(this).attr("order","false");
                $(this).find('img').attr("src","img/shop_list_5.png");
            }else{
                $(this).attr("order","true");
                $(this).find('img').attr("src","img/shop_list_4.png")
            }
        });
        price.click(function () {
            if($(this).attr("order")=="true"){
                sortObj(sortDatas,'price',true);
            }else{
                sortObj(sortDatas,'price',false);
            }
            createHtmlShop(sortDatas);
        });
        defaultSort.click(function(){
            if($(this).attr("order")=="true"){
                sortObj(sortDatas,'id',true);
            }else{
                sortObj(sortDatas,'id',false);
            }
            createHtmlShop(sortDatas);
        });
        $('#min,#max').keyup(function(){
            var text=$(this).val();
            text=integer(text);
            $(this).val(text);
        });
        sure.click(function(){
            goods.empty();
            numMin=$('#min').val();
            numMax=$('#max').val();
            sortDatas=priceSort(numMin,numMax);
            createHtmlShop(sortDatas);
        })
    }
    /*通过价格最小最大值筛选商品*/
    function priceSort(min,max){
        var shopLists = LsyStorage.getArr('shopList');
        var shopObj=[];
        shopLists.forEach(function(item){
            if(item.price>=min && item.price<=max){
                shopObj.push(item);
            }
        });
        return shopObj;
    }
    /*排序操作*/
    function sortObj(arr,property,bool) {
        var n = arr.length;
        var cache=null;
        var i=0,j=0;
        if(bool){
            for (i = 0; i < n; i++) {
                for (j = i; j < n; j++) {
                    if (arr[i][property] > arr[j][property]) {
                        cache = arr[i];
                        arr[i] = arr[j];
                        arr[j] = cache;
                    }
                }
            }
        }else {
            for (i = 0; i < n; i++) {
                for (j = i; j < n; j++) {
                    if (arr[i][property] < arr[j][property]) {
                        cache = arr[i];
                        arr[i] = arr[j];
                        arr[j] = cache;
                    }
                }
            }
        }
        return arr;
    }
    /*添加购物车*/
    function addCarEvent() {
        goods.on('click', '.shop-car', function () {
            var id = $(this).attr('data-id');
            var shopListDatas = LsyStorage.getItem('shopList_' + id);
            var datas = LsyStorage.getItem('shopCar_' + id);
            if (!datas) {
                shopListDatas.checked = true;
                shopListDatas.num = 1;
                LsyStorage.setItem('shopCar_' + id, shopListDatas);
                alert('已加入购物车');
            } else {
                datas.checked = true;
                datas.num++;
                LsyStorage.setItem('shopCar_' + id, datas);
                alert('已加入购物车');
            }
        })
    }
    /*判断Url中有没有search*/
    function judgeSearch(){
        var params=Params();
        if(params.search){
            var content=$('#content');
            var contentVal=params.search;
            contentVal=decodeURI(contentVal);
            content.val(contentVal);
            clickSearch(contentVal);
        }else {
            var shopDatas = LsyStorage.getArr('shopList');
            createHtmlShop(shopDatas);
        }
    }
    /*尾部点击搜索*/
    function footerSearch(){
        var huntSearch=$('#huntSearch');
        clickQuery(huntSearch,'#huntText')
    }
});
5